#include <algorithm>
#include <cstdio>
#define N 10001
using namespace std;
int t, m;
int v, w;
int f[100001];
int main() {
	freopen("testdata.in", "r", stdin);
	freopen("testdata.out", "w", stdout);
	scanf("%d%d", &t, &m);
	for (int i = 1; i <= m; ++i) {
		scanf("%d %d", &w, &v);
		for (int j = w; j <= t; ++j)
			f[j] = max(f[j - w] + v, f[j]);
	}
	printf("%d", f[t]);
	return 0;
}